﻿using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System;

namespace Sparrow.CMS.Providers
{
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.Shared)]
    [Export("default",typeof(IPageContentProvider))]
    public class MSSQLProvider:IPageContentProvider
    {
        public MSSQLProvider()
        {
            var connection = ConfigurationManager.ConnectionStrings["Sparrow"];
            if (connection == null)
            {
                throw new KeyNotFoundException("Sparrow in ConnectionStrings");
            }
            this.DataContext = new CMSDataContext(connection.ConnectionString);   
        }


        CMSDataContext DataContext
        {
            get;set;
        }
        

        #region ISparrow.CMS.PageContentProvider Members

        public System.Linq.IQueryable<Sparrow.CMS.PageContent> PageContents()
        {
            return this.DataContext.PageContents
                .Select(i => new Sparrow.CMS.PageContent 
                {
                    Id = i.Id,
                    Content = i.Content,
                    Culture = i.Culture,
                    Description = i.Description,
                    Heading = i.Heading,
                    Image = i.Image,
                    Keyword = i.Keyword,
                    LayoutName = i.LayoutName,
                    Title = i.Title
                });

        }

        public Sparrow.CMS.PageContent FindContent(Web.Router.WebRouter router, string culture)
        {
            return this.DataContext.PageContents
              .Where(i=>i.Culture == culture && i.RouterId == router.Id)
              .Select(i => new Sparrow.CMS.PageContent
              {
                  Id = i.Id,
                  Content = i.Content,
                  Culture = i.Culture,
                  Description = i.Description,
                  Heading = i.Heading,
                  Image = i.Image,
                  Keyword = i.Keyword,
                  LayoutName = i.LayoutName,
                  Title = i.Title
              })
              .FirstOrDefault();
        }

        public bool AddContent(Web.Router.WebRouter router, Sparrow.CMS.PageContent content)
        {
            if (this.DataContext.PageContents.Any(i => i.RouterId == router.Id && i.Culture == content.Culture))
            {
                return false;
            }
            else
            {
                content.Id = Guid.NewGuid().ToString();

                var item = new PageContent
                {
                    Id = content.Id,
                    Content = content.Content,
                    Culture = content.Culture,
                    Description = content.Description,
                    Heading = content.Heading,
                    Image = content.Image,
                    Keyword = content.Keyword,
                    LayoutName = content.LayoutName,
                    Title = content.Title,
                    RouterId = router.Id

                };

                this.DataContext.PageContents.InsertOnSubmit(item);
                this.DataContext.SubmitChanges();

                return true;
            }

        }

        public bool UpdateContent(Web.Router.WebRouter router, Sparrow.CMS.PageContent content)
        {
            var item = this.DataContext.PageContents
                .Where(i => i.RouterId == router.Id && i.Culture == content.Culture)
                .FirstOrDefault();

            if (item == null)
            {
                return false;
            }
            else
            {
                item.Id = content.Id;
                item.Content = content.Content;
                item.Culture = content.Culture;
                item.Description = content.Description;
                item.Heading = content.Heading;
                item.Image = content.Image;
                item.Keyword = content.Keyword;
                item.LayoutName = content.LayoutName;
                item.Title = content.Title;
                item.RouterId = router.Id;

                this.DataContext.SubmitChanges();

                return true;
            }
        }

        public bool RemoveContent(Web.Router.WebRouter router, Sparrow.CMS.PageContent content)
        {
            var item = this.DataContext.PageContents
               .Where(i => i.RouterId == router.Id && i.Culture == content.Culture)
               .FirstOrDefault();

            if (item == null)
            {
                return true;
            }
            else
            {
                this.DataContext.PageContents.DeleteOnSubmit(item);
                this.DataContext.SubmitChanges();

                return true;
            }

        }

        public System.Collections.Generic.IEnumerable<string> GetActionNames(Sparrow.CMS.PageContent content, Widget widget)
        {
            return this.DataContext.PageWidgetActions
                .Where(i => i.PageConentId == content.Id && i.WidgetName == widget.Name)
                .Select(i => i.ActionName);
        }

        public bool AddAction(Sparrow.CMS.PageContent content, Widget widget, string actionName)
        {
            if (this.DataContext.PageWidgetActions.Any(i => i.PageConentId == content.Id && i.WidgetName == widget.Name && i.ActionName == actionName))
            {
                return true;
            }
            else
            {
                var item = new PageWidgetAction
                {
                     PageConentId = content.Id,
                     WidgetName = widget.Name,
                     ActionName = actionName
                };

                this.DataContext.PageWidgetActions.InsertOnSubmit(item);
                this.DataContext.SubmitChanges();

                return true;
            }
        }

        public bool RemoveAction(Sparrow.CMS.PageContent content, Widget widget, string actionName)
        {
            var item = this.DataContext.PageWidgetActions
                .Where(i => i.PageConentId == content.Id && i.WidgetName == widget.Name && i.ActionName == actionName)
                .FirstOrDefault();

            if (item == null)
            {
                return true;
            }
            else
            {
                this.DataContext.PageWidgetActions.DeleteOnSubmit(item);
                this.DataContext.SubmitChanges();

                return true;
            }


        }

        #endregion
    }
}
